﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyCardAddControl : YuanlinUserControl
    {
        protected ValuedTextBox companyNameValuedTextBox;
        protected ValuedTextBox companyAddressValuedTextBox;
        protected ValuedTextBox companyContactPhoneValuedTextBox;
        protected ValuedTextBox companyPostCodeValuedTextBox;
        protected ValuedTextBox companyFaxNumberValuedTextBox;
        protected ValuedTextBox companyEmailValuedTextBox;
        protected ValuedTextBox companySiteValuedTextBox;
        protected ValuedTextBox companyQQValuedTextBox;
        protected ValuedTextBox companyMSNValuedTextBox;
        protected ValuedTextBox companyScaleValuedTextBox;
        protected ValuedTextBox companyTelephone2ValuedTextBox;
        protected ValuedTextBox companyTelephone1ValuedTextBox;
        protected CheckBoxList companyTypesCheckBoxList;
        protected ValuedDropDownList provinceDropDownList;
        protected ValuedDropDownList cityDropDownList;
        protected ValuedDropDownList townDropDownList;
        protected ValuedDropDownList smallTownDropDownList;
        protected ValuedEditor descriptionValuedEditor;
        protected ValuedTextBox greenNumberValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                BindData();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
            AjaxManager.Register(this, "AjaxManager");
        }

        private void BindData()
        {
            EntityList companys = BusinessManager.GetCompanyTypeList(RequestHelper.BuildGetCompanyTypeListRequest());
            if (companys != null)
            {
                BindCheckBoxList(companyTypesCheckBoxList, companys, "EntityId", "Title");
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = 0;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(provinceDropDownList, areas, "EntityId", "Title");
            if (this.provinceDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.provinceDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(cityDropDownList, areas, "EntityId", "Title");
            if (this.cityDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.cityDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(townDropDownList, areas, "EntityId", "Title");
            if (this.townDropDownList.SelectedValue.Length == 0)
                return;

            request = new AreaListRequest();
            request.UpAreaId = Convert.ToInt32(this.townDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas == null)
                return;
            BindDropDownList(smallTownDropDownList, areas, "EntityId", "Title");
        }

        private void BindDropDownList(ValuedDropDownList ddl, EntityList list, string valueField, string textField)
        {
            ddl.DataSource = list;
            ddl.DataValueField = valueField;
            ddl.DataTextField = textField;
            ddl.DataBind();
        }
        private void BindCheckBoxList(CheckBoxList cbl, EntityList list, string valueField, string textField)
        {
            cbl.DataSource = list;
            cbl.DataValueField = valueField;
            cbl.DataTextField = textField;
            cbl.DataBind();
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            Member member=MemberManager.GetMember(greenNumberValuedTextBox.Value);
            if (member == null)
            {
                Globals.ShowMessage(Page, "NoMember", "未找到填写的绿商号，请重新填写！");
                return;
            }
            CompanyCard companyCard = new CompanyCard();
            
            companyCard.UserId.Value = user.EntityId.Value;
            companyCard.CompanyUserId.Value = UserManager.GetUser(member.MemberId).EntityId.Value;
            companyCard.Flag.Value = (int)CompanyCardFlag.FavoritesCard;
            companyCard.CompanyName.Value =  CensorWordManager.CensorWord(this.companyNameValuedTextBox.Value);
            companyCard.CompanyAddress.Value =  CensorWordManager.CensorWord(this.companyAddressValuedTextBox.Value);
            companyCard.CompanyContactPhone.Value = this.companyContactPhoneValuedTextBox.Value;
            companyCard.CompanyPostCode.Value = this.companyPostCodeValuedTextBox.Value;
            companyCard.CompanyFaxNumber.Value = this.companyFaxNumberValuedTextBox.Value;
            companyCard.CompanyEmail.Value = this.companyEmailValuedTextBox.Value;
            companyCard.CompanySite.Value = this.companySiteValuedTextBox.Value;
            companyCard.CompanyQQ.Value = this.companyQQValuedTextBox.Value;
            companyCard.CompanyMSN.Value = this.companyMSNValuedTextBox.Value;
            companyCard.CompanyDescription.Value =  CensorWordManager.CensorWord(this.descriptionValuedEditor.Value);
            companyCard.CompanyScale.Value =  CensorWordManager.CensorWord(companyScaleValuedTextBox.Value);
            companyCard.CompanyFixedTelephone1.Value = companyTelephone1ValuedTextBox.Value;
            companyCard.CompanyFixedTelephone2.Value = companyTelephone2ValuedTextBox.Value;
            string companyTypes = string.Empty;
            foreach (ListItem item in companyTypesCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    companyTypes += "," + item.Value;
                }
            }
            if (companyTypes.Length > 0)
            {
                companyTypes = companyTypes + ",";
            }
            companyCard.CompanyTypes.Value = companyTypes;

            companyCard.Province.Value = Convert.ToInt32(provinceDropDownList.Value);
            companyCard.City.Value = Convert.ToInt32(cityDropDownList.Value);
            companyCard.Town.Value = Convert.ToInt32(townDropDownList.Value);
            companyCard.SmallTown.Value = Convert.ToInt32(smallTownDropDownList.Value);

            if (BusinessManager.CreateCompanyCard(companyCard) == -2)
            {
                Globals.ShowMessage(this.Page, "添加失败", "此信息已经存在！");
                return;
            }

            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyCardListUrl());
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetChildCategories(int parentCategoryID)
        {
            if (parentCategoryID == -1)
            {
                return string.Empty;
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = parentCategoryID;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));

            if (areas != null && areas.Count > 0)
            {
                string areaFormat = "{0}:{1}";
                string areaString = string.Empty;
                foreach (Area area in areas)
                {
                    if (areaString != string.Empty)
                    {
                        areaString += ";";
                    }
                    areaString += string.Format(areaFormat, area.EntityId.ToString(), area.Title);
                }
                return areaString;
            }

            return string.Empty;
        }
    }
}